package com.wetest.uia2.stub;

import android.os.Build;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityWindowInfo;

/* loaded from: classes9.dex */
class AccessibilityNodeInfoHelper {
    private static final String TAG = AccessibilityNodeInfoHelper.class.getSimpleName();

    private AccessibilityNodeInfoHelper() {
    }

    static android.graphics.Rect getVisibleBoundsInScreen(AccessibilityNodeInfo node, int width, int height, boolean trimScrollableParent) {
        return getVisibleBoundsInScreen(node, new android.graphics.Rect(0, 0, width, height), trimScrollableParent);
    }

    static android.graphics.Rect getVisibleBoundsInScreen(AccessibilityNodeInfo node, android.graphics.Rect displayRect, boolean trimScrollableParent) {
        if (node == null) {
            return null;
        }
        android.graphics.Rect nodeRect = new android.graphics.Rect();
        node.getBoundsInScreen(nodeRect);
        if (displayRect == null) {
            displayRect = new android.graphics.Rect();
        }
        intersectOrWarn(nodeRect, displayRect);
        if (Build.VERSION.SDK_INT >= 21) {
            android.graphics.Rect bounds = new android.graphics.Rect();
            AccessibilityWindowInfo window = Api21Impl.getWindow(node);
            if (window != null) {
                Api21Impl.getBoundsInScreen(window, bounds);
                intersectOrWarn(nodeRect, bounds);
            }
        }
        if (trimScrollableParent) {
            AccessibilityNodeInfo ancestor = node.getParent();
            while (true) {
                if (ancestor == null) {
                    break;
                }
                if (!ancestor.isScrollable()) {
                    ancestor = ancestor.getParent();
                } else {
                    android.graphics.Rect ancestorRect = getVisibleBoundsInScreen(ancestor, displayRect, true);
                    intersectOrWarn(nodeRect, ancestorRect);
                    break;
                }
            }
        }
        return nodeRect;
    }

    private static void intersectOrWarn(android.graphics.Rect target, android.graphics.Rect bounds) {
        if (!target.intersect(bounds)) {
            android.util.Log.v(TAG, String.format("No overlap between %s and %s. Ignoring.", target, bounds));
        }
    }

    static class Api21Impl {
        private Api21Impl() {
        }

        static void getBoundsInScreen(AccessibilityWindowInfo accessibilityWindowInfo, android.graphics.Rect outBounds) {
            accessibilityWindowInfo.getBoundsInScreen(outBounds);
        }

        static AccessibilityWindowInfo getWindow(AccessibilityNodeInfo accessibilityNodeInfo) {
            return accessibilityNodeInfo.getWindow();
        }
    }
}